//
//  FHHomeTableViewController.swift
//  WeiBo
//
//  Created by qfh on 16/7/9.
//  Copyright © 2016年 qiufuhe. All rights reserved.
//

import UIKit



class FHHomeTableViewController: FHVisitorTableViewController {

//    var statusList: [FHStatus]?
    
   lazy var statuListsModelView: FHStatusListViewModel = FHStatusListViewModel()
    
    

    //  下拉刷新
    lazy var pullDownView: FHRefreshControl = {
        
        let refrest = FHRefreshControl()
        
        refrest.addTarget(self, action: "pullDownRefresh", forControlEvents: .ValueChanged)
        
        
        return refrest
    }()
    
    
    // 下拉刷新
    
    private lazy var pullUpView: UIActivityIndicatorView = {
        
        let pullView = UIActivityIndicatorView(activityIndicatorStyle: .WhiteLarge)
        
        
        pullView.backgroundColor = UIColor.redColor()
        
        
        
        return pullView
        
        
    }()
    
    // tip label
    
    private lazy var tipLabel: UILabel = {
    
        let label = UILabel(textColor: UIColor.whiteColor(), size: 12)
        
        label.backgroundColor = UIColor.orangeColor()
    
        label.textAlignment = .Center
        
        label.text = "没有加载到微博"
        
        label.hidden = true
    
        return label
    }()
    
   
    
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        if isLogin {
            
           
            
            setUpTableView()
            
            setupUI()
            
            loadStats()
            
            
            
        }else {
            visitorView?.updateVisitorView(nil, imageName: nil)
        }
        
    }
    
    
    // 下拉刷新
    @objc func pullDownRefresh() {
        

        
        loadStats()
    
    }
    
    private func setupUI() {
    
        if let nav = self.navigationController {
        
            nav.view.insertSubview(tipLabel, belowSubview: nav.navigationBar)
            
            tipLabel.frame = CGRect(x: 0, y: 64 - 30, width: ScreenWidth, height: 30)
        
        }
    
    
    }
    
    
    
    /**
     设置 tableViwe
     */
    func setUpTableView() {
        tableView.registerClass(FHStatusTableViewCell.self, forCellReuseIdentifier: "HomeTableViewCellIdentifier")
        
        tableView.separatorStyle = .None
        
        tableView.allowsSelection = false
        
        tableView.rowHeight = UITableViewAutomaticDimension
        
        tableView.estimatedRowHeight = 300
        

        tableView.tableFooterView = pullUpView
        
        tableView.addSubview(pullDownView)
    }
    
    /**
     加载数据
     */
    
    func loadStats() {
        
        statuListsModelView.loadStatus(pullUpView.isAnimating()) {(isSuccess, message) -> () in
            
            
            if !self.pullUpView.isAnimating() {
                
                self.startAnimation(message)
            }
           
            
            self.stopAnimation()
            
            if isSuccess {
            
                self.tableView.reloadData()
                
            }
            
            
        }
        
    }
    
    // 加载完成 停止动画
    
    private func stopAnimation() {
        
        
        
        pullUpView.stopAnimating()
        
        pullDownView.endRefreshing()
        
       
    }
    
    /**
     开启加载动画
     
     - parameter message: <#message description#>
     */
    private func startAnimation(message: String) {
        
        if tipLabel.hidden == false {
        
            return
        }
        
        tipLabel.text = message
    
        tipLabel.hidden = false
        
        UIView.animateWithDuration(1, animations: { () -> Void in
            
            self.tipLabel.transform = CGAffineTransformMakeTranslation(0, self.tipLabel.height)
            
            }, completion: { (_) -> Void in
                
                UIView.animateWithDuration(1, animations: { () -> Void in
                    
                    self.tipLabel.transform = CGAffineTransformMakeTranslation(0, -self.tipLabel.height)
                    
                    }, completion: { (_) -> Void in
                        
                        
                        self.tipLabel.hidden = true
                        
                })
                
                
        })
        
        

    }

}

// MARK: - 代理

extension FHHomeTableViewController {

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
        return statuListsModelView.statusLists.count ?? 0
    
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier("HomeTableViewCellIdentifier", forIndexPath: indexPath) as! FHStatusTableViewCell
        
        let statusListViewModel = statuListsModelView.statusLists[indexPath.row]
        
        cell.statusViewModel = statusListViewModel
        
        return cell
    }

    
    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        
        
        
        if indexPath.row == ((statuListsModelView.statusLists.count) - 1) {
        
            pullUpView.startAnimating()
            
            loadStats()
        
        
        }
    }




}
